﻿// contntesManager
(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "wrapTipBox"

    // initialize object
    var wt = $.wrapTipBox = {

        // default values
        defaults: {
            direction: 'up'
        },

        setDefaults: function (defaultOptions) {
            this.defaults = $.extend({}, this.defaults, defaultOptions);
        }
    };

    $.fn.wrapTipBox = function (options) {

        // method calling
        if (typeof options == 'string') {
            var args = Array.prototype.slice.call(arguments, 1),
			    res;
            this.each(function () {
                var data = $.data(this, ui_name);
                if (data) {
                    var meth = data[options];
                    if (meth) {
                        var r = meth.apply(this, args);
                        if (res === undefined) {
                            res = r;
                        }
                    }
                }
            });
            if (res !== undefined) {
                return res;
            }
            return this;
        }

        // initialize options
        options = $.extend({}, wt.defaults, options);

        this.each(function () {
            var _self = this;


            // initialize callback
            for (var key in options) {
                var value = options[key];
                if (typeof value == 'function') {
                    // bind callbacks
                    $(_self).bind(key, value);
                }

            }

            /* Private Methods
            -----------------------------------------------------------------------------*/

            if (_self) {
                wrapTipBox(options.tipId);
            }


            function wrapTipBox(tipId) {
                var isWrapped = $(_self).data('tipboxWraped');
                if (!isWrapped) {
                    $(_self).data('tipboxWraped', true);
                }
                else {
                    // 已经wrap过了 忽略
                    return;
                }


                var tip = $(tipId).clone(true);
                var tipBox = $('<div class="tip"/>')
                  .append(tip.show())
                  .insertAfter(_self);

                if (options.direction == 'up') {
                    $(tip).append(_self);
                } else if (options.direction == 'down') {
                    $(tip).prepend(_self);
                }
            }

            /* Callback Triggers
            -----------------------------------------------------------------------------*/




            /* Public Methods
            -----------------------------------------------------------------------------*/

            var publicMethods = {
                'unwrapTipBox': function () {
                    var isWrapped = $(_self).data('tipboxWraped');
                    if (isWrapped) {
                        $(_self).data('tipboxWraped', null);
                    }
                    else {
                        // 还没wrpa过 忽略
                        return;
                    }

                    var tipParent = $(_self).parent().parent();
                    var content = $(_self).clone(true);
                    $(_self).insertAfter(tipParent);
                    $(tipParent).remove();
                }


                // @other public methods
            };

            $.data(this, ui_name, publicMethods);


            // @private methods


            // define callback triggers

            //function select() {
            //    var val = 12345;
            //    $(_self).trigger('select', val);
            //}

        });

        return this;
    };
    // @static methods

})(jQuery);